//Documentation complete
/**
 * @author Alex Peterson
 * @version 2009MR07
 */

package ui.internal.ajax;

import control.*;
import problemDomain.*;
import static utilities.XMLTools.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Handles all requests for menu item maintenance functions.
 */
public class MaintainFoodItem extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = 765927028458381204L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */
	
	@Override
	protected void doPost (HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession(false);
		response.setContentType("text/xml");
		if (session != null) {
			//EmployeeAccountBean account = (EmployeeAccountBean)session.getAttribute("account");
			//if (account.getPosition() != EmployeeAccountBean.Position.MANAGER) {
				String action = request.getParameter("a");
				if (action != null) {
					if (action.equalsIgnoreCase("c"))
						create(request, response);
					else if (action.equalsIgnoreCase("r"))
						report(request, response);
					else if (action.equalsIgnoreCase("u"))
						update(request, response);
					else if (action.equalsIgnoreCase("d"))
						delete(request, response);
					else if (action.equalsIgnoreCase("l"))
						listAll(response);
					else if (action.equalsIgnoreCase("t"))
						listItemTypes(response);
				} else
					response.setStatus(HttpServletResponse.SC_NO_CONTENT);
			//} else
			//	response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		} else
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
	}

	/* END (HttpServlet) Methods */
	
	
	/* Local Methods */
	
	private void create (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("name");
		String description = request.getParameter("description");
		String cost = request.getParameter("cost");
		String type = request.getParameter("type");
		String available = request.getParameter("available");
		
		PrintWriter out = response.getWriter();
		try {
			FoodItemControl miCtrl = new FoodItemControl();
			miCtrl.create(name, description, cost, type, available);
			out.write(ErrorBean.getSuccess().toXML());//"Item created"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}

	private void report (HttpServletRequest request, HttpServletResponse response) throws IOException {
		//String itemNumber = request.getParameter("itemNumber");
		String name = request.getParameter("name");
		String type = request.getParameter("type");
		String available = request.getParameter("available");

		PrintWriter out = response.getWriter();
		try {
			//String sItemNumber, String name, String type, String sAvailable
			FoodItemControl miCtrl = new FoodItemControl();
			List<FoodItemBean> reportList = miCtrl.search(name, type, available);
			out.write(encapsulateBeanListXML(reportList,"fooditems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void update (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String description = request.getParameter("description");
		String cost = request.getParameter("cost");
		String type = request.getParameter("type");
		String available = request.getParameter("available");
		
		PrintWriter out = response.getWriter();
		try {
			//String sid,String sItemNumber, String name, String description, String sCost, String sPrice, String type, String sAvailable
			FoodItemControl miCtrl = new FoodItemControl();
			miCtrl.update(id, name, description, cost, type, available);
			out.write(ErrorBean.getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}

	private void delete (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		try {
			FoodItemControl itemCtrl = new FoodItemControl();
			itemCtrl.delete(id);
			out.write(ErrorBean.getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listAll (HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			FoodItemControl itemCtrl = new FoodItemControl();
			List<FoodItemBean> list = itemCtrl.listAll();
			out.write(encapsulateBeanListXML(list, "fooditems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listItemTypes (HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			FoodItemControl itemCtrl = new FoodItemControl();
			List<FoodItemTypeBean> list = itemCtrl.listItemTypes();
			out.write(encapsulateBeanListXML(list, "orderitemtypes"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/* END Local Methods */
	
}
